package cn.edu360.day07;

import cn.edu360.day07.bean.AddrList;
import cn.edu360.day07.bean.BikeBean;
import cn.edu360.day07.bean.ResultBean;
import cn.edu360.day07.utile.Utiles;
import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by root on 2018/7/9.
 */
public class TestMain {
    //获取hashcode以及对应的位置信息
    static Map<String, String> map = loadData();

    public static void main(String[] args) {


        //读取bikes.log数据
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader("d:\\data\\bikes.log"))) {
            String line = null;
            while((line = bufferedReader.readLine())!=null){
                //json解析数据
                BikeBean bikeBean = JSON.parseObject(line, BikeBean.class);
                //获取经纬度信息
                double lng = bikeBean.getLongitude();
                double lat = bikeBean.getLatitude();
                //查找位置信息
                String ret = findAddr(lng, lat);
                System.out.println(ret);
            }

        }catch (Exception e){

        }

    }

    /**
     * 通过经纬度信息查找地址
     * 1：本地查找
     * 2：网络查找
     */
    public static String findAddr(double lng,double lat){
        //从本地查找
        String addrByLocal = findAddrByLocal(lng, lat);
        if(addrByLocal==null){
            //从网络中查找
            addrByLocal = findAddrByNet(lng, lat);
        }
        return  addrByLocal;
    }

    /**
     * 从网络中查找
     * @param lng
     * @param lat
     * @return
     */
    public static String findAddrByNet(double lng,double lat){
        String json = Utiles.getJsonByHttpClient(lng, lat);
        ResultBean resultBean = JSON.parseObject(json, ResultBean.class);
        //System.out.println(resultBean);
        AddrList[] addrList = resultBean.getAddrList();
        AddrList addrList1 = addrList[0];
        String name = addrList1.getName();
        String admName = addrList1.getAdmName();
        if(name!=null&&!"".equals(name)&&admName!=null&&!"".equals(admName)){
            String geoHashCode = Utiles.getGeoHashCode(lng, lat);
            String ret = geoHashCode+","+admName+","+name;

            //从网络上获取到数据
            try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("d:\\data\\reportery.txt",true))) {
                bufferedWriter.write(ret);
                bufferedWriter.newLine();
                //map添加一份
                map.put(geoHashCode,ret);
            }catch (Exception e){

            }


            return ret;
        }
        return null;

    }

    /**
     * 从本地查找数据
     * @param lng
     * @param lat
     * @return
     */
    public static String findAddrByLocal(double lng,double lat){
        String hashCode = Utiles.getGeoHashCode(lng, lat);
        String addr = map.get(hashCode);
        return addr;
    }


    /**
     * 加载数据  形成hashcode-->位置信息
     */
    public static Map<String, String> loadData(){
        Map<String,String> map = new HashMap<>();
        try (BufferedReader br = new BufferedReader(new FileReader("d:\\data\\reportery.txt"))) {
            String line = null;
            while((line = br.readLine())!=null){
                //System.out.println(line);
                String[] split = line.split(",");
                String hash = split[0];
                map.put(hash,line);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

}
